package demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TemplateEngine {

    public static String render(String template, Map<String, Object> context) {
        StringBuilder result = new StringBuilder();

        int startIndex = 0;
        while (startIndex < template.length()) {
            int openIndex = template.indexOf("{%", startIndex);
            int closeIndex = template.indexOf("%}", startIndex);
            if (openIndex != -1 && closeIndex != -1) {
                // Add the text before the tag to the result
                result.append(template.substring(startIndex, openIndex));

                // Extract the tag and evaluate it
                String tag = template.substring(openIndex + 2, closeIndex).trim();
                String[] parts = tag.split("\\s+");
                String keyword = parts[0];

                if (keyword.equals("if")) {
                    String condition = parts[1];
                    boolean isTrue = evaluateCondition(condition, context);
                    if (isTrue) {
                        result.append(render(getBlock(template, openIndex, closeIndex), context));
                    }
                } else if (keyword.equals("else")) {
                    result.append(render(getBlock(template, openIndex, closeIndex), context));
                } else if (keyword.equals("while")) {
                    String condition = parts[1];
                    while (evaluateCondition(condition, context)) {
                        result.append(render(getBlock(template, openIndex, closeIndex), context));
                    }
                } else if (keyword.equals("for")) {
                    String variable = parts[1];
                    List<Object> list = (List<Object>) context.get(parts[3]);
                    for (Object item : list) {
                        context.put(variable, item);
                        result.append(render(getBlock(template, openIndex, closeIndex), context));
                    }
                }

                // Move the start index to the end of the tag
                startIndex = closeIndex + 2;
            } else {
                // Add the remaining text to the result
                result.append(template.substring(startIndex));
                break;
            }
        }

        return result.toString();
    }

    private static boolean evaluateCondition(String condition, Map<String, Object> context) {
        String[] parts = condition.split("\\s+");
        if (parts[1].equals("==")) {
            Object left = context.get(parts[0]);
            Object right = context.get(parts[2]);
            return left.equals(right);
        } else {
            throw new IllegalArgumentException("Unsupported operator: " + parts[1]);
        }
    }

    private static String getBlock(String template, int openIndex, int closeIndex) {
        int startIndex = template.indexOf("{%", openIndex + 2);
        int endIndex = template.lastIndexOf("%}", closeIndex - 1);
        return template.substring(startIndex + 2, endIndex).trim();
    }

    public static void main(String[] args) {
        String template = "<ul>{% for item in items %}<li>{{ item }}</li>{% endfor %}</ul>";
        Map<String, Object> context = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("cherry");
        context.put("items",list);

        String result = render(template, context);
        System.out.println(result); // <ul><li>apple</li><li>banana</li><li>cherry</li></ul>
    }
}